package com.nowoe.data.processor;

import com.nowoe.core.bean.BeanFactory;
import com.nowoe.core.bean.BeanPostProcessor;
import com.nowoe.core.proxy.TargetProxy;
import com.nowoe.data.annotation.Mapper;
import com.nowoe.data.sqlSession.SqlSession;

/**
 * @author 倚栏听风 2023/9/16 11:12
 */
public class MapperBeanPostProcessor implements BeanPostProcessor {
    @Override
    public void enhance(BeanFactory beanFactory, Object object) {
        if (object instanceof TargetProxy) {
            TargetProxy proxy = (TargetProxy) object;
            object = proxy.target();//获得原始类型
        }
        if (object instanceof String) {
            Class<?> aClass = null;
            try {
                aClass = Class.forName(object.toString());
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
            if (aClass.isAnnotationPresent(Mapper.class)) {
                SqlSession sqlSession = beanFactory.getBean(SqlSession.class);
                beanFactory.replace(aClass, sqlSession.getMapper(aClass));
            }
        }

    }
}
